# -*- coding: utf-8 -*-


class CsUserInfoSql:
    def __init__(self, cs_db):
        self.cs_db = cs_db
        self.table = 'cs.cs_user_info'

    def get_cs_user_info(self, neme_id):
        if str(neme_id).isdigit():  # 判断输入的信息是cs名还是id
            sql = """select * from {table} where Rcustomerno ='{customer_id}';""".format(customer_id=neme_id,
                                                                                         table=self.table)
        else:
            sql = """select * from {table} where username ='{user_name}';""".format(user_name=neme_id, table=self.table)
        try:
            result = self.cs_db.read_sql_query(sql)  # 执行sql语句
        except Exception as e:
            raise Exception(f'由于数据连接异常，获取用户信息失败,{str(e)}')
        if not result.empty:
            # des = [item[0] for item in self.cursor.description]  # 获取数据表表头
            # user_info = result[0]
            # info_dict = dict(zip(des, user_info))
            info_dict = result.to_dict(orient='records')[0]
            return info_dict
        else:
            return {}
        # for item in result:  # 获取结果
        #     return {'cs_username': item[1], 'cs_password': item[2], 'customer_id': item[3]}

    def add_token(self, exp_times_tamp, token_time, token_str, customer_id):
        sql = """update {table} set token = '{token}',exp_time = '{exp_time}',exp_times_tamp = '{exp_times_tamp}' where Rcustomerno ='{Rcustomerno}';""".format(
            table=self.table, token=token_str, exp_time=token_time, exp_times_tamp=exp_times_tamp,
            Rcustomerno=customer_id)

        try:
            self.cs_db.operation(sql)  # 执行sql语句
        except Exception as e:
            raise Exception(e)


if __name__ == '__main__':
    csuserinfo = CsUserInfoSql()
    # info = csuserinfo.get_cs_user_info('admin')
    info = csuserinfo.get_cs_user_info('2119')
    print(info)
